package org.springblade.modules.door.xiaomai;

import com.alibaba.fastjson.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springblade.core.tool.api.R;
import org.springblade.modules.door.service.XiaomaiService;
import org.springblade.modules.door.vo.XiaomaiResponseVO;
import org.springblade.modules.door.dto.OrderNotifyDto;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@AllArgsConstructor
@RequestMapping("/xiaomai")
@Api(value = "软件升级-设备app版本表", tags = "设备app版本表接口")
@Slf4j
public class OrderController {
	@Resource
	private XiaomaiService xiaomaiService;

	/**
	 * 详情
	 */
	@PostMapping("/orderNotify")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "小麦订单回调", notes = "小麦订单回调")
	public XiaomaiResponseVO orderNotify(HttpServletRequest request) {
		try {
			String s = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
			log.info("小麦订单回调{}", s);
			OrderNotifyDto orderNotifyDto = JSON.parseObject(s, OrderNotifyDto.class);

			Boolean a = xiaomaiService.orderNotify(orderNotifyDto);

		} catch (IOException e) {
			e.printStackTrace();
		}
		XiaomaiResponseVO xiaomaiResponseVO = new XiaomaiResponseVO();
		xiaomaiResponseVO.setResultCode("666");
		xiaomaiResponseVO.setReturnCode("666");
		xiaomaiResponseVO.setResultMsg("success");
		xiaomaiResponseVO.setReturnMsg("success");
		return xiaomaiResponseVO;
	}



	/**
	 * 详情
	 */
	@PostMapping(value = {"/closeNotify","/openNotify"})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "小麦关门回调", notes = "小麦订单回调")
	public XiaomaiResponseVO doorOperateNotify(HttpServletRequest request) {
		String s = "";
		try {
			s = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
			log.info("小麦设备开关门回调请求{}", request.getRequestURI());
			log.info("小麦设备开关门回调{}", s);
		} catch (IOException e) {
			e.printStackTrace();
		}
		boolean a = xiaomaiService.doorOperateNotify(s);
		XiaomaiResponseVO xiaomaiResponseVO = new XiaomaiResponseVO();
		xiaomaiResponseVO.setResultCode("666");
		xiaomaiResponseVO.setReturnCode("666");
		xiaomaiResponseVO.setResultMsg("success");
		xiaomaiResponseVO.setReturnMsg("success");
		return xiaomaiResponseVO;
	}

	/**
	 * 详情
	 */
	@GetMapping("/getOrderInfo")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "传入设备信息", notes = "传入设备信息")
	public R<Object> getOrderInfo(String deviceCode,String requestSerial) {
		return R.data(xiaomaiService.getOrderInfo(deviceCode,requestSerial));
	}


	/**
	 * 详情
	 */
	@GetMapping("/orderComplete")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "传入设备信息", notes = "传入设备信息")
	public R<Integer> orderComplete(String deviceCode,String orderCode) {
		return R.data(xiaomaiService.orderComplete(deviceCode,orderCode));
	}



	/**
	 * 详情
	 */
	@GetMapping("/getDoorState")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "传入设备信息", notes = "传入设备信息")
	public R<Object> getDoorState(String deviceCode,String requestSerial) {
		return R.data(xiaomaiService.getDoorState(deviceCode,requestSerial));
	}
}
